package com.stlm2.dems.service;

import com.stlm2.dems.dao.UserDao;
import com.stlm2.dems.entity.User;
import com.stlm2.util.Convert;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;

import com.stlm2.dems.entity.Config;
import com.stlm2.dems.dao.ConfigDao;
import com.stlm2.core.base.BaseDao;
import com.stlm2.core.base.impl.BaseServiceImpl;
import org.springframework.ui.ModelMap;

import java.util.ArrayList;
import java.util.List;

@Service("configService")
@Transactional(rollbackForClassName={"java.lang.Exception"}, propagation = Propagation.REQUIRED)
public class ConfigService extends BaseServiceImpl<Config> {

	@Resource
	private ConfigDao configDao;
	@Resource
	private UserDao userDao;

	@Override
	protected BaseDao <Config> getDao() {
		return configDao;
	}

	public  void test(){
       Config config = new Config();
		List<String> list = new ArrayList<String>();
		list.add("id");
		list.add("configName");
         configDao.addIntoRedis(config,list);

	}

	public ModelMap buildModel(Config config) {
		ModelMap model = new ModelMap();
		if(config != null){
			User u=userDao.getById(config.getCreateUserId());
			String username=u.getRealName()!=null?u.getRealName():u.getName();
			model.put("id", config.getId());
			model.put("configName",config.getConfigName());
			model.put("configValue",config.getConfigValue());
			model.put("createTime", Convert.toYMDHM(config.getCreateTime()));
			model.put("userName", username);
			model.put("updateTime", Convert.toYMDHM(config.getUpdateTime()));
			model.put("memo",config.getMemo()==null?"":config.getMemo());
		}
		return model;
	}
	public List<ModelMap> convert(List<Config> configs){
		List<ModelMap> list = new ArrayList<ModelMap>();
		if(configs != null && configs.size() > 0){
			for(Config cconfig : configs){
				ModelMap map = buildModel(cconfig);
				list.add(map);
			}
		}
		return list;
	}
}

